/* ****************************************************************************
 *
 * Copyright (c) Microsoft Corporation. 
 *
 * This source code is subject to terms and conditions of the Microsoft Public License. A 
 * copy of the license can be found in the License.html file at the root of this distribution. If 
 * you cannot locate the  Microsoft Public License, please send an email to 
 * dlr@microsoft.com. By using this source code in any fashion, you are agreeing to be bound 
 * by the terms of the Microsoft Public License.
 *
 * You must not remove this notice, or any other, from this software.
 *
 * THIS CODE AND INFORMATION ARE PROVIDED "AS IS" WITHOUT WARRANTY OF ANY
 * KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A
 * PARTICULAR PURPOSE.
 * ***************************************************************************/


#include "stdafx.h"
#include "PGLX.h"
#include "PGLXPpg.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif


IMPLEMENT_DYNCREATE(CPGLXPropPage, COlePropertyPage)


/////////////////////////////////////////////////////////////////////////////
// Message map

BEGIN_MESSAGE_MAP(CPGLXPropPage, COlePropertyPage)
	//{{AFX_MSG_MAP(CPGLXPropPage)
	ON_BN_CLICKED(IDC_BUTTON_BROWSEFILE, OnButtonBrowsefile)
	ON_BN_CLICKED(IDC_BUTTON_LOAD_URL, OnButtonLoadUrl)
	ON_BN_CLICKED(IDC_BUTTON_LOADFILE, OnButtonLoadfile)
	ON_BN_CLICKED(IDC_CHECK_AUTOZOOM, OnCheckAutozoom)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()


/////////////////////////////////////////////////////////////////////////////
// Initialize class factory and guid

IMPLEMENT_OLECREATE_EX(CPGLXPropPage, "PGLX.PGLXPropPage.1",
	0x8321aeb5, 0xffec, 0x42e6, 0xbe, 0xce, 0xa7, 0xdc, 0xac, 0x87, 0x5a, 0x46)


/////////////////////////////////////////////////////////////////////////////
// CPGLXPropPage::CPGLXPropPageFactory::UpdateRegistry -
// Adds or removes system registry entries for CPGLXPropPage

BOOL CPGLXPropPage::CPGLXPropPageFactory::UpdateRegistry(BOOL bRegister)
{
	if (bRegister)
		return AfxOleRegisterPropertyPageClass(AfxGetInstanceHandle(),
			m_clsid, IDS_PGLX_PPG);
	else
		return AfxOleUnregisterClass(m_clsid, NULL);
}


/////////////////////////////////////////////////////////////////////////////
// CPGLXPropPage::CPGLXPropPage - Constructor

CPGLXPropPage::CPGLXPropPage() :
	COlePropertyPage(IDD, IDS_PGLX_PPG_CAPTION)
{
	//{{AFX_DATA_INIT(CPGLXPropPage)
	m_sXMLString = _T("");
	m_sXMLFile = _T("");
	m_sXMLURL = _T("");
	m_bAutoZoom = TRUE;
	//}}AFX_DATA_INIT

	SetHelpInfo(_T("Plot Graphic Library ActiveX Control"), _T("PGLX.HLP"), 0);
}


/////////////////////////////////////////////////////////////////////////////
// CPGLXPropPage::DoDataExchange - Moves data between page and properties

void CPGLXPropPage::DoDataExchange(CDataExchange* pDX)
{
	//{{AFX_DATA_MAP(CPGLXPropPage)
	DDP_Text(pDX, IDC_EDIT_XMLSTRING, m_sXMLString, _T("XMLString") );
	DDX_Text(pDX, IDC_EDIT_XMLSTRING, m_sXMLString);
	DDP_Text(pDX, IDC_EDIT_FILE, m_sXMLFile, _T("XMLFile") );
	DDX_Text(pDX, IDC_EDIT_FILE, m_sXMLFile);
	DDP_Text(pDX, IDC_EDIT_URL, m_sXMLURL, _T("XMLURL") );
	DDX_Text(pDX, IDC_EDIT_URL, m_sXMLURL);
	DDP_Check(pDX, IDC_CHECK_AUTOZOOM, m_bAutoZoom, _T("AutoZoom"));
	DDX_Check(pDX, IDC_CHECK_AUTOZOOM, m_bAutoZoom);
	//}}AFX_DATA_MAP
	DDP_PostProcessing(pDX);
}


/////////////////////////////////////////////////////////////////////////////
// CPGLXPropPage message handlers

void CPGLXPropPage::OnButtonBrowsefile() 
{
	static TCHAR szFilter[] = _T("XML (*.xml)|*.xml||");

	// pFileDlg...
	CFileDialog pFileDlg(
		TRUE /* open */,
		_T("xml")	/* default extension */,
		_T("untitled")	/* initial pFilename in name box*/,
		OFN_HIDEREADONLY | OFN_EXPLORER | OFN_ENABLESIZING | OFN_FILEMUSTEXIST,
		szFilter);
	
	// Getting pFile name
	if (pFileDlg.DoModal()!=IDOK)
		return;

	m_sXMLFile = pFileDlg.GetPathName();

	SetControlStatus( IDC_EDIT_FILE, TRUE );

	UpdateData(FALSE);
}

void CPGLXPropPage::OnButtonLoadUrl() 
{
	UpdateData(TRUE);

	// loading...
	CStdioFile* pFile;

	TRY
	{
		CInternetSession session;
		pFile=session.OpenURL(m_sXMLURL);

		if (pFile)
		{
			ULONGLONG length= pFile->GetLength();
			pFile->Read(m_sXMLString.GetBuffer((UINT)length), (UINT)length);
			m_sXMLString.ReleaseBuffer();

			SetControlStatus( IDC_EDIT_URL, TRUE );
			SetControlStatus( IDC_EDIT_XMLSTRING, TRUE );

			delete pFile;
		}
	
		session.Close();
	}
	CATCH(CInternetException,e)
	{
		AfxMessageBox(_T("An exception occured while trying to load url."));
	}
	END_CATCH
}

void CPGLXPropPage::OnButtonLoadfile() 
{
   CStdioFile file;

   if (file.Open(m_sXMLFile, CFile::modeRead))
   {
		UINT length= (UINT)file.GetLength();
		file.Read(m_sXMLString.GetBuffer(length), length);
		m_sXMLString.ReleaseBuffer();
	
		SetControlStatus( IDC_EDIT_FILE, TRUE );
		SetControlStatus( IDC_EDIT_XMLSTRING, TRUE );
	}

   UpdateData(FALSE);
}

void CPGLXPropPage::OnCheckAutozoom() 
{
	SetControlStatus( IDC_CHECK_AUTOZOOM, TRUE );	
}
